package hrc.jetty.extension.handler;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Endpoint;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

import hrc.jetty.extension.component.ContextListener;
import hrc.jetty.extension.http.SoapHandler;
import hrc.jetty.extension.modules.ConfigInfo;
import hrc.jetty.extension.util.CommonLogger;

public class SoapContextHandler extends AbstractContextHandler {
	
	private File jarFile;
	private URLClassLoader classLoader;
	private List<ContextListener> contextListenerList=new ArrayList<ContextListener>();

	public SoapContextHandler(ConfigInfo configInfo,File jarFile) throws ContextException {
		super(configInfo);
		this.jarFile=jarFile;
		this.init();
		setHandler(new AbstractHandler() {
			private List<Endpoint> endpoints=new ArrayList<Endpoint>();
		
			
			@Override
			protected void doStart() throws Exception {
				for(Map.Entry<String, String> entry:SoapContextHandler.this.configInfo.getPathMap().entrySet()){
					Class clazz=classLoader.loadClass(entry.getValue());
					SoapHandler handler=(SoapHandler) clazz.newInstance();
					String path=entry.getValue();
					String address="http://localhost:"+SoapContextHandler.this.port+"/"+SoapContextHandler.this.name+"/"+path;
					CommonLogger.getLogger().debug("soap address:"+address);
					Endpoint e=Endpoint.publish(address, handler);
					endpoints.add(e);				
				}
			}

			@Override
			protected void doStop() throws Exception {
				for(Endpoint e:endpoints){
					e.stop();
				}
			}

			@Override
			public void handle(String arg0, Request arg1, HttpServletRequest arg2,
					HttpServletResponse arg3) throws IOException, ServletException {
			
			}
		});
	}
	
	private void init() throws ContextException {		
		initClassLoader();		
	
	}
	
	@SuppressWarnings("deprecation")
	private void initClassLoader() throws ContextException {
		ClassLoader parent = null;
        if (parent == null) {
            parent = this.getClass().getClassLoader();
            CommonLogger.getLogger().debug("parent =this.getClass().getClassLoader()");
        }
        if (parent == null) {
            parent = ClassLoader.getSystemClassLoader();
            CommonLogger.getLogger().debug(" parent = ClassLoader.getSystemClassLoader()");
        }
            
		try {
			classLoader=new URLClassLoader(new URL[]{jarFile.toURL()},parent);
		} catch (MalformedURLException e) {
			throw new ContextException(e.getMessage(),e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void doStart() throws Exception {
		super.doStart();
		
		List<String> listenerList=configInfo.getListeners();
		for(String className:listenerList){
			Class clazz=classLoader.loadClass(className);
			ContextListener listener=(ContextListener) clazz.newInstance();
			listener.contextInitialization(configInfo);
			contextListenerList.add(listener);
		}
		
		this.getHandler().start();
	}
	
	

	@Override
	public void doHandle(String arg0, Request arg1, HttpServletRequest arg2,
			HttpServletResponse arg3) throws IOException, ServletException {
		
	}

	@Override
	protected void doStop() throws Exception {
		super.doStop();
		
		this.getHandler().stop();
		
		for(ContextListener listener:contextListenerList){
			listener.contextDestroy(configInfo);
		}
	}


	public File getJarFile() {
		return jarFile;
	}
	
}
